﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotConferenceSql;
using Infragistics.UltraChart.Shared.Styles;

namespace WebInterface
{
    public partial class DiagrammeArticleParTheme : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack==false)
                LoadConference();

            LoadChartData();
        }

        private void LoadConference()
        {
            using (var query = new SqlQuery())
            {
                var select = new SelectQueryElement();
                select.Add("ID_Conference");
                select.Add("Nom");
                
                query.Elements.Add(select);

                query.Elements.Add(new FromTableQueryElement("Conference"));

                var command = query.BuildSqlCommand();

                try
                {
                    var dReader = command.ExecuteReader();

                    DropDownListConference.DataValueField = "ID_Conference";
                    DropDownListConference.DataTextField = "Nom";

                    DropDownListConference.DataSource = dReader;
                    DropDownListConference.DataBind();

                    dReader.Close();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Charge les données du graphique
        /// </summary>
        private void LoadChartData()
        {

            if (DropDownListConference.SelectedIndex != -1)
            {
                
                int idConference = Convert.ToInt32(DropDownListConference.SelectedValue);

                var chartData = new DataTable();

                using (var query = new SqlQuery())
                {

                    var select = new SelectQueryElement();
                    select.Add("Sujet");
                    select.Add("Count(*) as [nb]");

                    query.Elements.Add(select);

                    query.Elements.Add(new FromTableQueryElement("Article"));

                    query.Elements.Add(new CustomWhereStatement(string.Format("ID_Conference={0}", idConference)));

                    var groupBy = new GroupByQueryElement();
                    groupBy.Add("Sujet");

                    query.Elements.Add(groupBy);

                    var selectCommand = query.BuildSqlCommand();

                    try
                    {
                        var dataReader = selectCommand.ExecuteReader();


                        if (dataReader != null)
                        {
                            chartData.Load(dataReader);

                            dataReader.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.ToString());
                    }


                }

                chartNbArticleParTheme.DataSource = chartData;
                chartNbArticleParTheme.DataBind();

                chartNbArticleParTheme.TitleBottom.Text = string.Format("Conférence: {0}" ,DropDownListConference.SelectedItem.Text);


                LabelErreur.Visible = chartData.Rows.Count==0;
                chartNbArticleParTheme.Visible = chartData.Rows.Count != 0;

                LabelErreur.Text = "Aucun sujet disponible pour cette conférence";
                
            }
            else
            {
                LabelErreur.Text = "Aucune conférence sélectionnée";
                LabelErreur.Visible = true;
                chartNbArticleParTheme.Visible = false;
            }

        }
    }
}
